extends Node2D

@onready var cell_rect : ColorRect = $CellRect
var actors : Array[Node2D]
var enemys : Array[Node2D]
#网格组
var grid_array : Dictionary
#网格整体行列大小
var grid_size : Vector2 = Vector2(8,20)
#子网格大小
var grid_cell_size : float = 60.0
func _ready():
	global_position = Vector2(grid_cell_size * grid_size.x + 500.0,grid_cell_size * grid_size.y / 2) * -1
	for x in grid_size.x :
		for y in grid_size.y :
			var cr = cell_rect.duplicate()
			cr.size = Vector2.ONE * grid_cell_size
			cr.position = Vector2(x,y) * grid_cell_size
			cr.show()
			grid_array[Vector2(x,y)] = GridXY.new(to_global(Vector2(x,y) * grid_cell_size))
			add_child(cr)
	owner.grid = self
	pass

func _get_xy() -> Vector2:
	var xy = floor((get_global_mouse_position() - global_position) / grid_cell_size)
	return xy
	
func _check_in_grid(xy : Vector2) -> bool:
	return grid_array.has(xy)

func _get_pos(xy : Vector2) -> Vector2 :
	var grid_xy = grid_array[xy]
	return grid_xy.xy
	
func _get_centor_pos(xy : Vector2) -> Vector2 :
	var grid_xy = grid_array[xy]
	return Vector2(grid_xy.xy.x + (grid_cell_size / 2),grid_xy.xy.y + (grid_cell_size / 2))

func _check_mouse_in_grid() -> bool:
	return grid_array.has(_get_xy())
